package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo4 {
    private static final String SQL_QUERY = "select id, name, salary, title from teacher";

    public static void main(String[] args) {
        try (
                Connection connection = DBUtil.getConnection();
                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery(SQL_QUERY)
        ) {
            processResultSet(rs);
        } catch (SQLException e) {
            // 更详细的异常处理
            System.err.println("数据库操作出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
    private static void processResultSet(ResultSet rs) throws SQLException {
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            double salary = rs.getDouble("salary");
            String title = rs.getString("title");
            System.out.println(id + " " + name + " " + salary + " " + title);
        }
    }
}
